//
//  InXiugaiViewController.m
//  Inner  Voice
//
//  Created by brother on 16/3/23.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "InXiugaiViewController.h"
#import "MBProgressHUD+BWMExtension.h"

@interface InXiugaiViewController ()<UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *passwordFieldText;
//菊花
@property (nonatomic,strong)MBProgressHUD *hud;
//注册中
@property(nonatomic,assign)BOOL registing;
//返回用户的id
@property(nonatomic,copy)NSString *userId;
@end

@implementation InXiugaiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (IBAction)finishBtnClick:(id)sender {
    if (self.passwordFieldText.text.length < 6||self.passwordFieldText.text.length >20) {
        [self showAlertView:@"密码的长度不符合规定"];
    }
    if((self.passwordFieldText.text.length >= 6 && self.passwordFieldText.text.length <= 20)){
        
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            [self sendRegistInfo];
        
    }

}

#pragma make -  发送修改信息
- (void)sendRegistInfo{
    if ([AppDelegate shareAppdelegate].canConnectedToTheInternet) {
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.view.userInteractionEnabled = NO;
   //  UserModel *user = [AppDelegate shareAppdelegate].userModel;
//    NSDictionary *dic = @{@"tel":user.tel,@"newpassword":self.passwordFieldText.text};
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        NSString *user_id = [userDefault valueForKey:@"u_id"];
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:user_id forKey:@"tel"];
        [dic setValue:self.passwordFieldText.text forKey:@"newpassword"];
        
    NSString *url = @"http://wangtest11.applinzi.com/home/Banner/updateUser";
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    manger.responseSerializer = [AFJSONResponseSerializer serializer];
    [manger POST:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"_____________修改____________%@",[responseObject[@"code"] class]);
        if([responseObject isKindOfClass:[NSDictionary class]]){
            
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
            if ([str isEqualToString:@"1"]) {
//                NSString *str = responseObject[@"data"];
//                self.userId = str;
//                UserModel *user = [[UserModel alloc]init];
//                user.u_id = str;
//                user.password = self.passwordFieldText.text;
//                
//                [AppDelegate shareAppdelegate].userModel = user;
                
               // [self showAlertView:@"修改成功"];
                
                InMainTabBarViewController *con = [[InMainTabBarViewController alloc]init];
                [self presentViewController:con animated:YES completion:nil];
                
            }else{
                [self showAlertView:@"修改失败"];
            }
        }
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"注册%@",error);
        self.view.userInteractionEnabled = YES;
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [self showAlertView:@"网络连接失败"];
    }];
    }else {
        [self showAlertView:@"当前网络不可用"];
    }
}
#pragma mark - 封装UIAlertView方法，直接调用就可以显示提示
-(void)showAlertView:(NSString *)string {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:string delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
    
}

//封装UIAlertView方法，直接调用就可以显示提示
//-(void)showAlertView:(NSString *)string withDelegate:(id<UIAlertViewDelegate>)delegate withTag:(NSInteger)tagCount {
//    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:string delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//    alertView.delegate = delegate;
//    alertView.tag=tagCount;
//    [alertView show];
//}

//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//    if (buttonIndex == 1) {
//        NSLog(@"修改用户信息");
//        //返回用户主页
//        InMainTabBarViewController *con = [[InMainTabBarViewController alloc]init];
//        [self presentViewController:con animated:YES completion:nil];
//    }
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
